<?php
/**
*  
*/
class LoginController extends Controller
{
	public function actionLogin(){
		if( isset($_POST['username']) && isset($_POST['password']) ){
			$username = $_POST['username'];
			$pass = $_POST['password'];

			$user = UserManager::model()->findByPk($username);
			// print_r($user);die();
			if($user){
				if($pass === $user->password){
					$date = new CDbExpression('NOW()');
					$user->last_login = $date;
					$user->save();
					echo json_encode(array("statusLogin" => 'success',"level"=>$user->level));
				}else{
					echo json_encode(array("statusLogin" => 'fail'));
				}
			}else{
				echo json_encode(array("statusLogin" => 'username does not exits'));
			}
		}
	}
	public function actionGetInfoUser(){
		$username = $_POST['username'];
		$user = UserManager::model()->findByPk($username);
		if($user){
			$fullname = $user->fullname;
			$last_login = $user->last_login;
			$username = $user->username;
			$level = $user->level;

			$role_user = RoleUser::model()->findAll('username=:us', array(':us'=>$username));

			$chucvu = array();
			foreach ($role_user as $key => $value) {
				$roleid = $value->role_id;
				$role = Roles::model()->findByPk($roleid);
				$chucvu[$key] = $role->attributes; 
			}

			echo json_encode(array('username'=>$username,'fullname'=>$fullname,'last_login'=>$last_login,'level'=>$level,
				'roles'=>$chucvu));
		}
	}
}